/*
Assignment 1
Class ID        : SE0691
Group           : 1
Due Date        : 20 November 2012
I declare that this assignment is my own work
in accordance with FPT Policy.
*/
package edu.fpt.tag;

import edu.fpt.dal.ServicesDAL;
import edu.fpt.hib.Services;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 *
 * @author Admin
 */
public class ServicesListTag extends SimpleTagSupport {

    /**
     * Called by the container to invoke this tag. The implementation of this
     * method is provided by the tag library developer, and handles all tag
     * processing, body iteration, etc.
     */
    @Override
    public void doTag() throws JspException, IOException {
        JspWriter out = getJspContext().getOut();

        try {
            // TODO: insert code to write html before writing the body content.
            // e.g.:
            //
            // out.println("<strong>" + attribute_1 + "</strong>");
            // out.println("    <blockquote>");

            JspFragment f = getJspBody();
            if (f != null) {
                f.invoke(out);
            }
            out.println("<table id=\"mytable\" cellspacing=\"0\">");
            out.println("<tr>");
            out.println("<th scope=\"col\" class=\"nobg\">Services ID</th>"
                    + "<th scope=\"col\">Services Name</th>"
                    + "<th scope=\"col\">Services Price</th>"
                    + "<th scope=\"col\">Edit</th>"
                    + "<th scope=\"col\">Delete</th>"
                    + "</tr>");

            ArrayList<Services> list = ServicesDAL.listAll();
            for (Services services : list) {

                out.println("<tr><th scope=\"row\" class=\"spec\">" + services.getServiceId() + "</th>");
                out.println("<td class=\"alt\">" + services.getServiceName() + "</td>");
                out.println("<td class=\"alt\">" + services.getServicePrice() + "</td>");
                out.println("<td class=\"alt\"><form method=\"get\" action=\"editservice.jsp\">");
                out.println("<input type=\"hidden\" name=\"serviceid\" value=\"" + services.getServiceId() + "\">");
                out.println("<input class=\"searchsubmit\" type=\"submit\" value=\"Edit\">");
                out.println("</form></td>");
                out.println("<td class=\"alt\" ><form method=\"get\" action=\"deleteservice.jsp\">");
                out.println("<input type=\"hidden\" name=\"serviceid\" value=\"" + services.getServiceId() + "\">");
                out.println("<input class=\"searchsubmit\" type=\"submit\" value=\"Delete\">");
                out.println("</form></td>");
                out.println("</tr>");
            }
            out.println("</table>");

            // TODO: insert code to write html after writing the body content.
            // e.g.:
            //
            // out.println("    </blockquote>");

        } catch (java.io.IOException ex) {
            throw new JspException("Error in ServicesListTag tag", ex);
        }
    }
}
